window.onload = function () {
    var swiper = new Swiper('.swiper-container', {
        effect: 'fade',
        spaceBetween: 30,
        centeredSlides: true,
        // loop: true,
        autoplay: {
            delay: 3000,
            disableOnInteraction: false,
        },
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        on:{
            init: function(){
                $('.swiperguidev3-item').eq(this.activeIndex).addClass('active').siblings().removeClass("active")
            }, 
            slideChangeTransitionEnd: function(){
                $('.swiperguidev3-item').eq(this.activeIndex).addClass('active').siblings().removeClass("active")
            }
        }
    });
    $('.swiperguidev3-item').mouseover(function(){
        $(this).addClass('active').siblings().removeClass('active')
        swiper.slideTo($(this).index(), 1000, false);
    })
}
var app = new Vue({
    el: '.container',
    computed: {
        exchangenum: {//兑换数量
            get() {
                if (!isNaN(Number(this.exchange.exchangenum))) {
                    return this.exchange.exchangenum
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0) {
                        this.exchange.exchangenum = 0
                    } else {
                        this.exchange.exchangenum = parseFloat(v)
                    }
                    if (parseFloat(Number(v)) > parseFloat(this.userinfo.banlance)) {
                        this.exchange.exchangenum = parseInt(this.userinfo.banlance)
                    }
                    this.exchange.exchangenum = Math.floor(this.exchange.exchangenum * 100) / 100
                    let computedresult = parseFloat(Number(this.exchange.exchangenum)) * this.exchange.agratenum
                    if (!isNaN(Number(computedresult))) {
                        this.exchange.exchangeagcoin = computedresult
                        if (this.exchange.exchangeagcoin > 3000) {
                            this.exchange.disexchangenum = true
                            let lang = this.getCookie("lang");
                            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                            var b = '<div class="maskLayer"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                                this.exchange.disexchangenum = false
                                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                                document.querySelector('.container').removeChild(c)
                                document.querySelector('.container').removeChild(d)
                            }
                            if (lang == "en_US") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "The amount is greater than 3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "The maximum redemption limit for AG points is 3000"
                            } else if (lang == "zh_TW") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "額度大於3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "AG分最大兌換額度為3000"
                            } else {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "额度大于3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "AG分最大兑换额度为3000"
                            }
                        }
                    } else {
                        this.exchange.exchangeagcoin = 0
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        },
        cashoutnum: { //兑出数量
            get() {
                if (!isNaN(Number(this.cashout.exchangenum))) {
                    return this.cashout.exchangenum
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0 || this.userinfo.myagbanlance == '0') {
                        this.cashout.exchangenum = 0
                    } else {
                        this.cashout.exchangenum = parseInt(v)
                        if (parseFloat(Number(v)) > parseFloat(this.userinfo.myagbanlance)) {
                            this.cashout.exchangenum = parseInt(this.userinfo.myagbanlance)
                        }
                        this.cashout.exchangenum = Math.floor(this.cashout.exchangenum * 100) / 100
                        let computedresult = parseFloat(Number(this.cashout.exchangenum)) / parseFloat(this.exchange.agratenum)
                        computedresult = parseFloat(computedresult.toFixed(8))
                        if (!isNaN(Number(computedresult))) {
                            this.cashout.exchangeagcoin = computedresult
                        } else {
                            this.cashout.exchangeagcoin = 0
                        }
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        }
    },
    data() {
        return {
            HPS: "HPS",
            ETH: "ETH",
            BTC: "BTC",
            HSR: "HSR",
            HAND: "HAND",
            HART: "HART",
            MOT: "MOT",
            showtransforminag: false,//兑换AG分
            showtransformout: false,//兑出AG分
            showText: false,
            hideText: true,
            exchange: { //兑换配置项
                defaultcoin: 'HPS', //默认币种
                defaultcoin2: 'HPS', //PC主页币种
                isshowcoinarr: false, //是否展示下拉选
                isshowcoinarr2: false, //PC主页点击切换币种
                exchangenum: '0', //兑换数量
                disexchangenum: false,//禁用兑换数量
                exchangeagcoin: '0',//兑换后数量
                agratenum: 5, //AG兑换比例
                disabledbtn: false //禁用兑换点击按钮
            },
            cashout: { //兑出配置项
                exchangenum: '0', //兑换数量
                exchangeagcoin: '0',//兑换后币种数量
                disabledbtn: false //禁用兑出点击按钮
            },
            userinfo: { //用户信息
                banlance: '',//用户余额
                myagbanlance: 0,//我的ag积分余额
            },
            theuser: "",
            information: "",
            loading: false
        }
    },
    methods: {
        gotohyperdice() { //头部前去dice
            if (localStorage.getItem('ID') != null || localStorage.getItem('ID') != '') {
                if(this._mime("type", "application/vnd.chromium.remoting-viewer")){
                    let dialogopt={
                        zh_CN:{
                            title1:'友情提示',
                            title2:'弹出窗口若被阻拦，请在右上角或浏览器设置里允许显示弹出式窗口'
                        },
                        zh_TW:{
                            title1:'友情提示',
                            title2:'彈出窗口若被阻攔，請在右上角或瀏覽器設置裏允許顯示彈出式窗口'
                        },
                        en_US:{
                            title1:'Help',
                            title2:'If pop-ups are blocked, allow pop-ups in the upper-right corner or browser settings'
                        }
                    }
                    this.opendialog(dialogopt)
                }
                axios.post('/user/setredis', {}).then((res) => {
                    if (res.data.code == 200) {
                        if (res.data.data != 'fail') {
                            let userkey = res.data.data
                            let url = 'http://dice.play.top/?key='
                            window.open(`${url}${userkey}`,'_self')
                        } else {
                            location.href = '/singin'
                        }
                    } else {
                        location.href = '/singin'
                    }
                })
            } else {
                location.href = '/singin'
            }
        },
        filtertime(num) { //过滤时间
            let times = new Date(num)
            let month = times.getMonth() + 1
            let day = times.getDate()
            if (month < 10) {
                month = '0' + month
            }
            if (day < 10) {
                day = '0' + day
            }
            return `${month}-${day}`
        },
        getagBalance() {//获取当前用户AG分
            let params = {
                coin: this.exchange.defaultcoin
            }
            axios.post('/ag/agBalance', params).then((res) => {
                if (res.data.code == 200) {
                    if (res.data.data) {
                        this.userinfo.myagbanlance = res.data.data
                    }
                }
            })
        },
        changetransform(keyname) { //用户点击兑换、兑出AG分栏目
            if (!keyname) {
                return
            }
            this.$data[keyname] = !this.$data[keyname]
            if (keyname == 'showtransforminag') { //兑换AG分
                this.showtransformout = false
                if (this.$data[keyname]) {
                    this.getagDict(this.exchange.defaultcoin) //根据币种换算兑换比例
                }
            } else if (keyname == 'showtransformout') {//兑出AG分
                this.showtransforminag = false
            }
        },
        changeexchangecoin(cointype, banlance) {//用户点击更换币种
            this.exchange.defaultcoin = cointype
            this.exchange.defaultcoin2 = cointype
            this.exchange.isshowcoinarr = false
            this.exchange.isshowcoinarr2 = false
            this.userinfo.banlance = banlance
            this.getagDict(cointype)
            this.getagBalance()
        },
        getagDict(coinname) { //选择币种获取兑换比例
            if (!coinname) {
                return
            }
            axios.get(`/ag/dict/${coinname}`, {}).then((res) => {
                if (res.data.data.dictValue) {
                    this.exchange.agratenum = parseFloat(res.data.data.dictValue)
                }
            })
        },
        confirmexchange() { //确定兑换AG分
            //为空、NaN、
            if (
                this.exchange.exchangenum == '' || isNaN(Number(this.exchange.exchangenum)) || this.exchange.exchangenum == 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌換提示',
                        title2:'請輸入兌換數量大於零'
                    },
                    zh_CN:{
                        title1:'兑换提示',
                        title2:'请输入兑换数量大于零'
                    },
                    en_US:{
                        title1:'Exchange tips',
                        title2:'Redemption quantity is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            if (parseFloat(Number(this.exchange.exchangenum)) > parseFloat(this.userinfo.banlance)) { //兑换数量>用户余额
                let lang = this.getCookie("lang");
                var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                var b = '<div class="maskLayer"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                document.getElementsByClassName("closeinfo")[0].onclick = () => {
                    var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                    var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                    document.querySelector('.container').removeChild(c)
                    document.querySelector('.container').removeChild(d)
                }
                if (lang == "en_US") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "Lack of balance"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "Insufficient account balance"
                } else if (lang == "zh_TW") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "餘額不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "賬戶餘額不足！"
                } else {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "余额不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "账户余额不足！"
                }
                return
            }
            this.exchange.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.exchange.exchangenum)
            }
            axios.post('/ag/account/buyAgCredit', params).then((res) => {
                this.exchange.disabledbtn = false
                if (res.data.code == 200) {
                    let dialogopt={
                        zh_TW:{
                            title1:'兌換成功',
                            title2:'AG積分兌換成功！'
                        },
                        zh_CN:{
                            title1:'兑换成功',
                            title2:'AG积分兑换成功！'
                        },
                        en_US:{
                            title1:'Successfully redeemed',
                            title2:'Successfully converted the AG points.'
                        }
                    }
                    this.opendialog(dialogopt)
                    if (!this.getCookie('firsttransforminag')) {
                        this.addCookie('firsttransforminag', new Date().getTime(), 'Infinity')
                    }
                    this.getagBalance()
                }
            })
        },
        getCookie(cookiename) { //获取cookie
            var arr, reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if (arr = document.cookie.match(reg)) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        addCookie(objName, objValue, objDays) { //设置cookie
            var str = objName + "=" + escape(objValue);
            if (objDays > 0) {
                var date = new Date();
                var ms = objDays * 24 * 3600 * 1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            if (objDays === Infinity) {
                str += "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
            }
            str += "; path=/";
            document.cookie = str;
        },
        closeinfo() { //关闭广告窗
            document.getElementsByClassName('maskLayer')[0].style.display = "none"
            document.getElementsByClassName('errorMessage')[0].style.display = "none"
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        enteraggame(gameType = null) { //点击进入游戏
            //无token||无ID
            if (
                // isNaN(Number(this.userinfo.myagbanlance))||
                // parseFloat(this.userinfo.myagbanlance)<=0||
                localStorage.getItem('mytoken') == null ||
                localStorage.getItem('mytoken') == '' ||
                localStorage.getItem('ID') == '' ||
                localStorage.getItem('ID') == ''
            ) {
                location.href = '/singin'
                return
            }
            // 我的ag积分余额小于0||非数字||
            if (
                isNaN(Number(this.userinfo.myagbanlance)) ||
                parseFloat(this.userinfo.myagbanlance) <= 0
            ) {
                let lang = this.getCookie("lang");
                var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                var b = '<div class="maskLayer"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                document.getElementsByClassName("closeinfo")[0].onclick = function () {
                    var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                    var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                    document.querySelector('.container').removeChild(c)
                    document.querySelector('.container').removeChild(d)
                }
                if (lang == "en_US") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "Insufficient points"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "Your lack of AG points"
                } else if (lang == "zh_TW") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "積分不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "您的AG積分不足"
                } else {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "积分不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "您的AG积分不足"
                }
                return
            }
            let params = {
                coin: this.exchange.defaultcoin,
                gameType: gameType ? gameType : 8, //AG大厅
                token: localStorage.getItem('mytoken'), //用户令牌
                isMobile: false //pc
            }
            axios.post('/ag/getGameUrl', params).then((res) => {
                if (res.data.code == 200) {
                    let form = document.createElement('form')
                    form.setAttribute('method', 'post')
                    form.setAttribute('class', 'gotoagGame')
                    form.setAttribute('action', res.data.data)
                    form.setAttribute('target', '_blank')
                    document.body.appendChild(form)
                    document.getElementsByClassName('gotoagGame')[0].submit()
                    document.body.removeChild(document.getElementsByClassName('gotoagGame')[0])
                }
            })
        },
        confircashout() { //从AG兑回
            if (
                localStorage.getItem('ID') == '' ||
                localStorage.getItem('ID') == ''
            ) {
                location.href = '/singin'
                return
            }
            if (
                this.userinfo.myagbanlance == '' ||
                this.cashout.exchangenum == 0 ||
                this.cashout.exchangenum == '' ||
                isNaN(Number(this.userinfo.myagbanlance)) ||
                parseFloat(this.userinfo.myagbanlance) <= 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌回提示',
                        title2:'請輸入兌回數量大於零'
                    },
                    zh_CN:{
                        title1:'兑回提示',
                        title2:'请输入兑回数量大于零'
                    },
                    en_US:{
                        title1:'Redemption tips',
                        title2:'The amount of redemption is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            this.cashout.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.cashout.exchangenum)
            }
            axios.post('/ag/transferFromAg', params).then((res) => {
                this.cashout.disabledbtn = false
                if (res.data.code == 200) {
                    if (res.data.data) {
                        this.getagBalance()
                    }
                }
            })
        },
        REturnTOp() {
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = "hidden"
            let scrollToptimer = setInterval(function () {
                var top = document.body.scrollTop || document.documentElement.scrollTop;
                var speed = top / 4;
                if (document.body.scrollTop != 0) {
                    document.body.scrollTop -= speed;
                } else {
                    document.documentElement.scrollTop -= speed;
                }
                if (top == 0) {
                    clearInterval(scrollToptimer);
                    let BODY = document.getElementsByTagName("body")[0]
                    BODY.style.overflowY = ""
                }
            }, 30);
        },
        xxtea_encrypt(str, key) {
            if (str == "") {
                return "";
            }
            var v = this.str2long(str, true);
            var k = this.str2long(key, false);
            if (k.length < 4) {
                k.length = 4;
            }
            var n = v.length - 1;

            var z = v[n], y = v[0], delta = 0x9E3779B9;
            var mx, e, p, q = Math.floor(6 + 52 / (n + 1)), sum = 0;
            while (0 < q--) {
                sum = sum + delta & 0xffffffff;
                e = sum >>> 2 & 3;
                for (p = 0; p < n; p++) {
                    y = v[p + 1];
                    mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                    z = v[p] = v[p] + mx & 0xffffffff;
                }
                y = v[0];
                mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                z = v[n] = v[n] + mx & 0xffffffff;
            }
            return this.long2str(v, false);
        },
        long2str(v, w) {
            var vl = v.length;
            var n = (vl - 1) << 2;
            if (w) {
                var m = v[vl - 1];
                if ((m < n - 3) || (m > n)) return null;
                n = m;
            }
            for (var i = 0; i < vl; i++) {
                v[i] = String.fromCharCode(v[i] & 0xff,
                    v[i] >>> 8 & 0xff,
                    v[i] >>> 16 & 0xff,
                    v[i] >>> 24 & 0xff);
            }
            if (w) {
                return v.join('').substring(0, n);
            }
            else {
                return v.join('');
            }
        },
        str2long(s, w) {
            var len = s.length;
            var v = [];
            for (var i = 0; i < len; i += 4) {
                v[i >> 2] = s.charCodeAt(i)
                    | s.charCodeAt(i + 1) << 8
                    | s.charCodeAt(i + 2) << 16
                    | s.charCodeAt(i + 3) << 24;
            }
            if (w) {
                v[v.length] = len;
            }
            return v;
        },
        opendialog(obj) { //打开遮罩层
            let lang = this.getCookie("lang");
            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
            var b = '<div class="maskLayer"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                document.querySelector('.container').removeChild(c)
                document.querySelector('.container').removeChild(d)
            }
            document.getElementsByClassName('errorMessageTop')[0].innerHTML = obj[lang].title1
            document.getElementsByClassName('errorMessageBottom')[0].innerHTML = obj[lang].title2
        },
        _mime(option, value) { //判断是否是360浏览器
            var mimeTypes = navigator.mimeTypes;
            for (var mt in mimeTypes) {
                if (mimeTypes[mt][option] == value) {
                    return true;
                }
            }
            return false;
        }
    },
    mounted() {
        if (localStorage.getItem('ID') != null || localStorage.getItem('ID') != '') {
            this.getagBalance()
        }
        this.loading = true
    }
})